package com.cyjt.operation.fragment;


import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import java.util.Set;

import com.cyjt.operation.R;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link BTDevicesSelectFragment.OnBTDevicesSelectionListener} interface
 * to handle interaction events.
 * Use the {@link BTDevicesSelectFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BTDevicesSelectFragment extends Fragment {

    private OnBTDevicesSelectionListener mListener;

    public static BTDevicesSelectFragment newInstance() {
        BTDevicesSelectFragment fragment = new BTDevicesSelectFragment();
        return fragment;
    }

    public BTDevicesSelectFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void reFreashPairedDevices(Set<BluetoothDevice> pairedDevices) {
        if (layout_btdevices_paired != null)
            layout_btdevices_paired.removeAllViews();
            for (BluetoothDevice device : pairedDevices) {
                addPairedDevice(device);
        }
    }

    public void addPairedDevice(BluetoothDevice btD) {

        deviceView = (RelativeLayout) inflater.inflate(R.layout.layout_bt_device, null);
        deviceView.setTag(btD);
        deviceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onBTDevicesFBeSelected((BluetoothDevice) v.getTag());
            }
        });
        ((TextView) deviceView.findViewById(R.id.textView_title)).setText(btD.getName() + "\n" + btD.getAddress());
        layout_btdevices_paired.addView(deviceView);
    }

    public void addNewDevice(BluetoothDevice btD) {
        deviceView = (RelativeLayout) inflater.inflate(R.layout.layout_bt_device, null);
        deviceView.setTag(btD);
        deviceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onBTDevicesFBeSelected((BluetoothDevice) v.getTag());
            }
        });
        ((TextView) deviceView.findViewById(R.id.textView_title)).setText(btD.getName() + "\n" + btD.getAddress());
        layout_btdevices_new.addView(deviceView);
    }

    public void discoveryFinished() {
        textView_refreash_button.setText(getString(R.string.ui_text_bluetooth_search));
        textView_refreash_button.setClickable(true);
    }


    public void doDiscovery() {
        textView_refreash_button.setText(getString(R.string.ui_text_bluetooth_searching));
        textView_refreash_button.setClickable(false);
    }

    public void clearDevicesViews() {
        if (layout_btdevices_paired != null)
            layout_btdevices_paired.removeAllViews();
        if(layout_btdevices_new !=null)
            layout_btdevices_new.removeAllViews();
    }

    private View rootView;
    private LinearLayout layout_btdevices_paired;
    private LinearLayout layout_btdevices_new;
    private LayoutInflater inflater;
    private RelativeLayout deviceView;
    private TextView textView_refreash_button;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        this.inflater = inflater;
        rootView = inflater.inflate(R.layout.fragment_btdevices_select, container, false);
        layout_btdevices_paired = (LinearLayout) rootView.findViewById(R.id.layout_btdevices_paired);
        layout_btdevices_new = (LinearLayout) rootView.findViewById(R.id.layout_btdevices_new);
        rootView.findViewById(R.id.textView_close_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onBTDevicesFBeClosed();
            }
        });
        textView_refreash_button = (TextView) rootView.findViewById(R.id.textView_refreash_button);
        textView_refreash_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onBTDevicesFBeRefreashed();
            }
        });
        return rootView;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnBTDevicesSelectionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnBTDevicesSelectionListener {

        public void onBTDevicesFBeClosed();

        public void onBTDevicesFBeRefreashed();

        public void onBTDevicesFBeSelected(BluetoothDevice bD);
    }

}
